package com.action.park;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.log4j.Logger;

import com.bean.SysUser;
import com.dao.ParkOrderDao;
import com.service.PMWithdrawalService;
import com.util.GUtil;
import com.bean.DrawMoneyRequestRecord;
public class PMWithdrawalAction extends BaseParkAction {
	
	private static final Logger log = Logger.getLogger(ParkOrderDao.class);
	
	SimpleDateFormat sdf = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss ");
	
	PMWithdrawalService service;
	DrawMoneyRequestRecord record;
	List<DrawMoneyRequestRecord>list;
	@Override
	public String handle() throws Exception {
		if (id == null)
			return this.query();
		else
			return this.view();
	}

	@Override
	public String query() throws Exception {
		this.setHeaderTitle("收入提取");
		list=service.querydrawmoney(record, user, page);
		return GUtil.SUCCESS;
	}
	/*
	 * 接受处理
	 */
	public String okhandle() throws Exception {
		this.setHeaderTitle("接受处理");
		if (id == null)
			return GUtil.ERROR;
		if (record == null) {
			// beforeUpdate
			record = (DrawMoneyRequestRecord) service.get(Long.valueOf(id));
			if (record == null) {
				this.setLogmsg("信息不存在 id=" + id);
				return GUtil.ERROR;
			}
			session.put("okrecord", record);
			return GUtil.EDIT;
		}
		// update
		DrawMoneyRequestRecord dr = (DrawMoneyRequestRecord) session.get("okrecord");
		Timestamp ts = new Timestamp(System.currentTimeMillis());
		dr.setProcessTime(ts);
		dr.setStatus((short) 2);
		dr.setComment(record.getComment());
		sysuser = (SysUser) session.get("sysuser");
		dr.setSysUser(sysuser);
		if(service.update(dr).endsWith(GUtil.SUCCESS)){
			this.setMsg("操作成功");
			this.setLogmsg("[" + sdf.format(ts) + "][停车场超级管理员:" + user.getUserCode() + ","
					+ user.getUserName() + "][接受并处理了停车场管理员:"
					+ dr.getParkManager().getRealName()+ "的车场提款申请," + "记录id:"
					+ dr.getId()+ "]");
			dr = null;
			record = null;
			return this.query();
		}else{
			this.setMsg("操作失败");
			this.setLogmsg("[" + sdf.format(ts)+ "][停车场超级管理员:" + user.getUserCode() + ","
					+ user.getUserName() + "][对停车场管理员:"
					+ dr.getParkManager().getRealName()+ "的车场提款申请处理失败" + "记录id:"
					+ dr.getId()+ "]");
			return this.query();
		}

	}
	/*
	 * 拒绝处理
	 */
	public String nohandle() throws Exception {
		this.setHeaderTitle("拒绝处理");
		if (id == null)
			return GUtil.ERROR;
		if (record == null) {
			// beforeUpdate
			record = (DrawMoneyRequestRecord) service.get(Long.valueOf(id));
			if (record == null) {
				this.setLogmsg("信息不存在 id=" + id);
				return GUtil.ERROR;
			}
			session.put("okrecord", record);
			return GUtil.EDIT;
		}
		// update
		DrawMoneyRequestRecord dr = (DrawMoneyRequestRecord) session.get("okrecord");
		Timestamp ts = new Timestamp(System.currentTimeMillis());
		dr.setProcessTime(ts);
		dr.setStatus((short) -1);
		dr.setComment(record.getComment());
		if(service.update(dr).endsWith(GUtil.SUCCESS)){
			this.setMsg("操作成功");
			this.setLogmsg("[" + sdf.format(ts) + "][停车场超级管理员:" + user.getUserCode() + ","
					+ user.getUserName() + "][拒绝了停车场管理员:"
					+ dr.getParkManager().getRealName()+ "的车场提款申请," + "记录id:"
					+ dr.getId()+ "]");
			dr = null;
			record = null;
			return this.query();
		}else{
			this.setMsg("操作失败");
			this.setLogmsg("[" + sdf.format(ts) + "][停车场超级管理员:" + user.getUserCode() + ","
					+ user.getUserName() + "][对停车场管理员:"
					+ dr.getParkManager().getRealName()+ "的车场提款申请拒绝失败," + "记录id:"
					+ dr.getId()+ "]");
			return this.query();
		}
	}
	/*
	 * 汇款成功
	 */
	public String remitmoney() throws Exception {
		this.setHeaderTitle("确认汇款");
		if (id == null)
			return GUtil.ERROR;
		if (record == null) {
			// beforeUpdate
			record = (DrawMoneyRequestRecord) service.get(Long.valueOf(id));
			if (record == null) {
				this.setLogmsg("信息不存在 id=" + id);
				return GUtil.ERROR;
			}
			session.put("okrecord", record);
			return GUtil.EDIT;
		}
		// update
		DrawMoneyRequestRecord dr = (DrawMoneyRequestRecord) session.get("okrecord");
		Timestamp ts = new Timestamp(System.currentTimeMillis());
		dr.setProcessTime(ts);
		dr.setStatus((short) 3);
		dr.setComment(record.getComment());
		if(service.update(dr).endsWith(GUtil.SUCCESS)){
			this.setMsg("确认汇款成功");
			this.setLogmsg("[" + sdf.format(ts) + "][停车场超级管理员:" + user.getUserCode() + "确认汇款成功,"
					+ user.getUserName() + "][停车场管理员:"
					+ dr.getParkManager().getRealName()+ "," + "记录id:"
					+ dr.getId()+ "]");
			dr = null;
			record = null;
			return this.query();
		}else{
			this.setMsg("确认汇款失败");
			this.setLogmsg("[" + sdf.format(ts) + "][停车场超级管理员:" + user.getUserCode() + "确认汇款失败,"
					+ user.getUserName() + "][停车场管理员:"
					+ dr.getParkManager().getRealName()+ "的车场提款申请," + "记录id:"
					+ dr.getId()+ "]");
			return this.query();
		}
	}
	
	
	
	@Override
	public String delete() throws Exception {
		return null;
	}

	@Override
	public String view() throws Exception {
		if(id==null || id<1)
			return GUtil.ERROR;
		record =  (DrawMoneyRequestRecord) service.get(Long.valueOf(id));
		session.put("record", record);
		return GUtil.VIEW;
	}

	@Override
	public String add() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String update() throws Exception {
		
		return null;
	}

	@Override
	public boolean validate(Object obj) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	public PMWithdrawalService getService() {
		return service;
	}

	public void setService(PMWithdrawalService service) {
		this.service = service;
	}

	public DrawMoneyRequestRecord getRecord() {
		return record;
	}

	public void setRecord(DrawMoneyRequestRecord record) {
		this.record = record;
	}

	public List<DrawMoneyRequestRecord> getList() {
		return list;
	}

	public void setList(List<DrawMoneyRequestRecord> list) {
		this.list = list;
	}

}
